package com.ncd.form.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ncd.form.domain.Layout;
import com.ncd.form.domain.LayoutCell;
import com.ncd.form.domain.LayoutVersion;
import com.ncd.form.mapper.LayoutCellMapper;
import com.ncd.form.mapper.LayoutMapper;
import com.ncd.form.mapper.LayoutVersionMapper;
import com.ncd.form.service.ILayoutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表单名称Service业务逻辑处理
 *
 * @author tree_new_bee
 */
@Service
public class LayoutServiceIml extends ServiceImpl<LayoutMapper, Layout> implements ILayoutService {

    @Resource
    LayoutVersionMapper layoutVersionMapper;

    @Resource
    LayoutCellMapper layoutCellMapper;

    @Override
    public List<Layout> queryList(Layout layout) {
        LambdaQueryWrapper<Layout> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(layout.getStatus()),Layout::getStatus,layout.getStatus());
        wrapper.eq(StrUtil.isNotBlank(layout.getLayoutCode()),Layout::getLayoutCode,layout.getLayoutCode());
        wrapper.like(ObjectUtil.isNotNull(layout.getLayoutCode()),Layout::getLayoutCode,layout.getLayoutCode());
        wrapper.like(ObjectUtil.isNotNull(layout.getLayoutName()),Layout::getLayoutName,layout.getLayoutName());
        return this.list(wrapper);
    }

    @Override
    public int copyLayout(Layout lastLayout) {
        int ret=0;
        Long layoutId=lastLayout.getLayoutId();
        LayoutVersion layoutVersionParm=new LayoutVersion();
        layoutVersionParm.setLayoutId(layoutId);
        layoutVersionParm.setVersionStatus(2L);
        List<LayoutVersion> layoutVersions=layoutVersionMapper
                .selectList(new LambdaQueryWrapper<LayoutVersion>()
                        .eq(LayoutVersion::getLayoutId,layoutId)
                        .eq(LayoutVersion::getVersionStatus,2L));
        if(layoutVersions!=null){
            LayoutVersion lastLayoutVersion=layoutVersions.get(0);
            List<LayoutCell> layoutCells=layoutCellMapper
                    .selectList(new LambdaQueryWrapper<LayoutCell>()
                    .eq(LayoutCell::getCellVersionId,lastLayoutVersion.getVersionId()));

            //保存表单布局主表
            lastLayout.setLayoutId(null);
            ret=this.baseMapper.insert(lastLayout);
            //保存表单布局版本表
            LayoutVersion layoutVersion=new LayoutVersion();
            layoutVersion.setVersionStatus(lastLayoutVersion.getVersionStatus());
            layoutVersion.setLayoutId(lastLayout.getLayoutId());
            layoutVersion.setVersionNum(1L);
            layoutVersionMapper.insert(layoutVersion);
            //保存表单布局表
            Map<Long,Long> parentMap=new HashMap<>();
            for(LayoutCell lastLayoutCell:layoutCells){
                LayoutCell layoutCell=new LayoutCell();
                layoutCell.setCellLayout(lastLayoutCell.getCellLayout());
                layoutCell.setCellVersionId(layoutVersion.getVersionId());
                layoutCell.setCellStyle(lastLayoutCell.getCellStyle());
                layoutCell.setCellDict(lastLayoutCell.getCellDict());
                layoutCell.setCellType(lastLayoutCell.getCellType());
                if(parentMap.containsKey(lastLayoutCell.getCellParent())){
                    layoutCell.setCellParent(parentMap.get(lastLayoutCell.getCellParent()));
                }else{
                    layoutCell.setCellParent(0L);
                }
                layoutCellMapper.insert(layoutCell);
                parentMap.put(lastLayoutCell.getCellId(),layoutCell.getCellId());
            }
        }
        return ret;
    }
}
